import { for_obj } from 'sui';

const db_set = (k, v) => sessionStorage.setItem(k, v);

const db_set_obj = (obj) => for_obj(obj, (k, v) => db_set(k, v ? v : ""));

const db_set_json = (k, v) => sessionStorage.setItem(k, JSON.stringify(v));

const db_get_str = (k) => {
    const r = sessionStorage.getItem(k);
    if (r) {
        return r;
    }

    return "";
};

const db_get_json = (k) => {
    const ret = db_get_str(k);
    return JSON.parse(ret);
};

const db_get_int = (k) => {
    let ret = db_get_str(k);
    ret = parseInt(ret, 10);
    if (isNaN(ret)) {
        return 0;
    }

    return ret;
};

const db_get_bool = (k) => {
    const ret = db_get_str(k);
    return ret === "true";
};

const db_get_float = (k) => {
    let ret = db_get_str(k);
    return parseFloat(ret);
};

export {db_set_obj, db_get_bool, db_get_json, db_get_int, db_get_float, db_get_str, db_set, db_set_json};
